package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.ApiOrgApplyTianTianKaBao;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import com.gitee.apanlh.util.algorithm.encrypt.asymmetric.RSA;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 安心分期-天天卡包
 *
 * @author gwc
 * @create 2025-07-23 上午10:14
 */
@ApiPlan(productId = "369", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5TianTianKaBao implements ApiOrgPhoneMd5ConvertHandler {

    //  rsa公钥
    private static final String PUBLIC_KEY  = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAscqQXvPQJj1NkVb0K6kB" +
            "YvauVKwKtekQNldBPrpujeW96ZZGraFChUoWbruvCpqc9eEcK0pon1ca1uTjTRHY" +
            "F8rWp5gpsmTFhI5bEfl9y7t2n9hjQRISUCfeyDDETSvDufFfqKZ+O40B5fwMh7tu" +
            "TuXzE6BoXTfhbIgIGcu6iC/cljIpIOb27LiXAjHrP2rx4+UQDKVNKPkPKdWlvpVG" +
            "76bZxwkAYePydnwm1M+31Z7tPXhhkoL2mQHAAwjO6f5aY29CW6OQe3fufEgxCGgy" +
            "oml0fx7dgKq9ejuIRBhTB20uA/I40rQDwENGnmF9P0zevEluOdReeFZYG1Hlkjhe" +
            "VwIDAQAB";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        Map<Object, Object> phone = new LinkedHashMap<>();
        phone.put("phone_md5", apiDataExchangeParameter.getPhoneMd5());
        return phone;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        //  创建对应进件实体类
        ApiOrgApplyTianTianKaBao data = new ApiOrgApplyTianTianKaBao();
        //  加密方式
        RSA rsa = new RSA(PUBLIC_KEY, null);
        //  姓名（rsa加密）
        String encryptedName = rsa.encryptToBase64Str(apiApplyParameter.getUserName());
        data.setName(encryptedName);
        //  手机号（rsa加密后）
        String encryptedPhone = rsa.encryptToBase64Str(apiApplyParameter.getPhone());
        data.setPhone(encryptedPhone);
        //  城市
        data.setCity(apiApplyParameter.getCity());
        //  性别 0-男 1-女
        String sex = ChooseEq.create(apiApplyParameter.getSex())
                .when("0", "0")
                .when("1", "1")
                .end("0");
        data.setSex(sex);
        //  年龄
        data.setAge(apiApplyParameter.getAge());
        //  逾期情况：无逾期；当前有逾期；一年以内无逾期
        String overdue = ChooseEq.create(apiApplyParameter.getOverdue())
                .when("1", "无逾期")
                .when("2", "当前有逾期")
                .when("3", "一年以内无逾期")
                .end();
        data.setOverdue(overdue);
        //  车辆情况：无车产；有车不抵押；有车可抵押；有车产
        String carInfo = ChooseEq.create(apiApplyParameter.getCarInfo())
                .when("1", "无车产")
                .when("2", "有车不抵押")
                .when("3", "有车可抵押")
                .when("4", "有车产")
                .end();
        data.setCarInfo(carInfo);
        //  贷款金额：3万以下；5万-10万；10万-20万；20万以上
        String loanAmount = ChooseEq.create(apiApplyParameter.getLoanAmount())
                .when("1", "3万以下")
                .when("2", "5万-10万")
                .when("3", "20万")
                .when("4", "20万以上")
                .end();
        data.setLoanAmount(loanAmount);
        //  营业执照注册年限：未注册；注册1年以下；注册1~5年；注册5年以上
        String bizLicenseYear = ChooseEq.create(apiApplyParameter.getBizLicenseYear())
                .when("1", "未注册")
                .when("2", "注册1年以下")
                .when("3", "注册1~5年")
                .when("4", "注册5年以上")
                .end();
        data.setBizLicenseYear(bizLicenseYear);
        //  蚂蚁花呗可用额度：无额度；3千以下；3千-1万；1-3万；3万以上
        String hbLimit = ChooseEq.create(apiApplyParameter.getHbLimit())
                .when("1", "无额度")
                .when("2", "3千以下")
                .when("3", "3千-1万")
                .when("4", "1-3万")
                .when("5", "3万以上")
                .end();
        data.setHbLimit(hbLimit);
        //  芝麻分：600分以下；600~649分；650~699分；700分以上
        String sesameCredit = ChooseEq.create(apiApplyParameter.getSesameCredit())
                .when("1", "600分以下")
                .when("2", "600~649分")
                .when("3", "650~699分")
                .when("4", "700分以上")
                .end();
        data.setSesameCredit(sesameCredit);
        //  职业：上班族；自由职业；企业主（有营业执照）；公务员或事业单位；个体户
        String career = ChooseEq.create(apiApplyParameter.getCareer())
                .when("1", "上班族")
                .when("2", "自由职业")
                .when("3", "企业主（有营业执照）")
                .when("4", "公务员或事业单位")
                .when("5", "个体户")
                .end();
        data.setCareer(career);
        //  社保缴纳：无社保；缴纳6个月以下；缴纳6个月以上；有社保
        String socialSecurity = ChooseEq.create(apiApplyParameter.getSocialSecurity())
                .when("1", "无社保")
                .when("2", "缴纳6个月以下")
                .when("3", "缴纳6个月以上")
                .when("4", "有社保")
                .end();
        data.setSocialSecurity(socialSecurity);
        //  京东白条可用额度：无额度；3千以下；3千-1万；1-3万；3万以上
        String btLimit = ChooseEq.create(apiApplyParameter.getBtLimit())
                .when("1", "无额度")
                .when("2", "3千以下")
                .when("3", "3千-1万")
                .when("4", "1-3万")
                .when("5", "3万以上")
                .end();
        data.setBtLimit(btLimit);
        //  月收入：1000-3000；3000-8000；8000~15000；15000~30000；30000以上
        String monthlySalary = ChooseEq.create(apiApplyParameter.getMonthlySalary())
                .when("1", "1000-3000")
                .when("2", "3000-8000")
                .when("3", "8000~15000")
                .when("4", "15000~30000")
                .when("5", "30000以上")
                .end();
        data.setMonthlySalary(monthlySalary);
        //  房产情况：无房产；有房不抵押；有房可抵押；有房产；有按揭房产；有全款房产
        String realEstateInfo = ChooseEq.create(apiApplyParameter.getRealEstateInfo())
                .when("1", "无房产")
                .when("2", "有房不抵押")
                .when("3", "有房可抵押")
                .when("4", "有房产")
                .when("5", "有按揭房产")
                .when("6", "有全款房产")
                .end();
        data.setRealEstateInfo(realEstateInfo);
        //  总经营流水：10~50万元；50~100万元；100~500万元
        String bizBankDetails = ChooseEq.create(apiApplyParameter.getBizBankDetails())
                .when("1", "10~50万元")
                .when("2", "50~100万元")
                .when("3", "100~500万元")
                .end();
        data.setBizBankDetails(bizBankDetails);
        //  工资发放形式：银行代发打卡；现金收入；个人转账
        String salaryPayMethod = ChooseEq.create(apiApplyParameter.getSalaryPayMethod())
                .when("1", "银行代发打卡")
                .when("2", "现金收入")
                .when("3", "个人转账")
                .end();
        data.setSalaryPayMethod(salaryPayMethod);
        //  对公账户经营收入：10~50万元；50~100万元；100~500万元
        String bizPubAccBankDetails = ChooseEq.create(apiApplyParameter.getBizPubAccBankDetails())
                .when("1", "10~50万元")
                .when("2", "50~100万元")
                .when("3", "100~500万元")
                .end();
        data.setBizPubAccBankDetails(bizPubAccBankDetails);
        //  公积金：无公积金；缴纳6个月以下；缴纳6个月以上；有公积金
        String providentFund = ChooseEq.create(apiApplyParameter.getProvidentFund())
                .when("1", "无公积金")
                .when("2", "缴纳6个月以下")
                .when("3", "缴纳6个月以上")
                .when("4", "有公积金")
                .end();
        data.setProvidentFund(providentFund);
        //  信用卡额度：无信用卡；3千以下；3千-1万；1-3万；3万以上；有信用卡
        String creditLimit = ChooseEq.create(apiApplyParameter.getCreditLimit())
                .when("1", "无信用卡")
                .when("2", "3千以下")
                .when("3", "3千-1万")
                .when("4", "1-3万")
                .when("5", "3万以上")
                .when("6", "有信用卡")
                .end();
        data.setCreditLimit(creditLimit);
        //  保险保单：无保险保单；缴纳未满1年；缴纳1年以上；有保险保单
        String insurancePolicy = ChooseEq.create(apiApplyParameter.getInsurancePolicy())
                .when("1", "无保险保单")
                .when("2", "缴纳未满1年")
                .when("3", "缴纳1年以上")
                .when("4", "有保险保单")
                .end();
        data.setInsurancePolicy(insurancePolicy);
        //  借款用途：旅游；教育；婚庆；装修；医疗；个人消费
        String loanPurpose = ChooseEq.create(apiApplyParameter.getLoanPurpose())
                .when("1", "旅游")
                .when("2", "教育")
                .when("3", "婚庆")
                .when("4", "装修")
                .when("5", "医疗")
                .when("6", "个人消费")
                .end();
        data.setLoanPurpose(loanPurpose);
        //  单位工龄：0-6个月；6-12个月；12个月以上
        String yearsService = ChooseEq.create(apiApplyParameter.getYearsService())
                .when("1", "0-6个月")
                .when("2", "6-12个月")
                .when("3", "12个月以上")
                .end();
        data.setYearsService(yearsService);
        //  学历：初中及以下；中专；高中；大专；本科；研究生及以上
        String educationAttainment = ChooseEq.create(apiApplyParameter.getEducationAttainment())
                .when("1", "初中及以下")
                .when("2", "中专")
                .when("3", "高中")
                .when("4", "大专")
                .when("5", "本科")
                .when("6", "研究生及以上")
                .end();
        data.setEducationAttainment(educationAttainment);
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
